import {curlStore} from '@/stores/framework/curl'

const COLUMNS = [
    {field: "companyId", title: "公司编号", width: "220px", align: "left"},
    {field: "companyName", title: "公司名称", width: "220px", align: "left"},
    {field: "companyShortName", title: "公司简称", width: "150px", align: "left"},
    {field: "signBeginTime", title: "签约开始时间", width: '120px'},
    {field: "signEndTime", title: "签约结束时间", width: '120px'},
    {field: "companyAddress", title: "公司地址", minWidth: "320px", align: "left"},
    {field: "customerServiceNumber", title: "投诉电话", width: "120px"},
    {field: "createTime", title: "创建时间", width: "220px"},
]

const SEARCH_FORM = [{field: "companyName", title: "公司名称"}]

const OPTIONS = [
    {title: "修改", emit: "modify"},
    {title: "分账设置", emit: "billSetting"},
    {title: "进件", emit: "incoming"}
]

const API = {
    LIST: {method: "post", api: "/company/pageCompany"},
    SAVE: "/company/createCompany",
    MODIFY: "/company/updateCompany",
    INFO: "/company/getCompany"
}

const store = {
    state(){
    },

    actions:{
        /**
         * 修改
         * @param formData
         * @returns {Promise<boolean>}
         */
        async modify(formData) {
            Msg.loading("提交中...")
            let {code, data} = await Http.post(API.MODIFY + "/" + formData.companyId, formData);
            Msg.loading(false);
            if(code != 200) {
                Msg.error("修改失败");
                return false;
            }

            Msg.success("修改成功");
            this.refresh();
            return true;
        },

        /**
         * 调用详情接口
         * @param id
         * @returns {Promise<*>}
         */
        async getInfo(id) {
            Msg.loading("加载中...")
            let {code, data} = await Http.get(API.INFO + "/" + id);
            Msg.loading(false)
            if(code != 200) {
                return false
            }
            return data;
        },

        /**
         * 获取配置
         * @param id
         * @returns {Promise<boolean|*>}
         */
        async getConfig(id) {
            Msg.loading("加载中...")
            let {code, data} = await Http.get("/company/getCompanyConfig/" + id)
            Msg.loading(false);
            if(code != 200) {
                return false;
            }
            return Helper.emptyJson(data) ? {} : data;
        },

        /**
         * 保存分账比例配置
         * @param formData
         * @returns {Promise<boolean>}
         */
        async saveConfig(formData) {
            Msg.loading("加载中...")
            let {code, data} = await Http.post("/company/saveCompanyConfig/" + formData.companyId, formData)
            Msg.loading(false);
            if(code != 200) {
                return false;
            }
            Msg.success('保存成功')
            return true;
        },

        /**
         * 保存进件
         * @param form
         * @returns {Promise<boolean>}
         */
        async saveMerchantExpand(form) {
            Msg.loading("加载中...")
            let {code, data} = await Http.post("/company/saveMerchantExpand/" + form.companyId, form)
            Msg.loading(false);
            if(code != 200) {
                return false;
            }
            Msg.success("保存成功");
            return data;
        },

        /**
         * 获取进件
         * @param companyId
         * @returns {Promise<boolean>}
         */
        async getMerchantExpand(companyId) {
            Msg.loading("加载中...")
            let {code, data} = await Http.get("/company/getMerchantExpand/" + companyId)
            Msg.loading(false);
            if(code != 200) {
                return false;
            }

            if(Helper.emptyJson(data)) {
                return {};
            }
            return data;
        },

        /**
         * 进件预校验
         * @returns {Promise<void>}
         */
        async checkMerchantExpand(merchantExpandId) {
            Msg.loading("加载中...")
            let {code, data} = await Http.get("/company/merchantExpandConsult/" + merchantExpandId);
            Msg.loading(false);

            if(code != 200) {
                return false;
            }

            return data;
        },

        /**
         * 进件审核
         * @returns {Promise<void>}
         */
        async submitMerchantExpand(merchantExpandId) {
            Msg.loading("加载中...")
            let {code, data} = await Http.get("/company/merchantExpandSubmitAudit/" + merchantExpandId);
            Msg.loading(false);

            if(code != 200) {
                return false;
            }

            Msg.success("已提交审核");
            return true;
        },


        /**
         * 进件审核查询
         * @returns {Promise<void>}
         */
        async queryMerchantExpand(merchantExpandId) {
            Msg.loading("加载中...")
            let {code, data} = await Http.get("/company/merchantExpandAuditQuery/" + merchantExpandId);
            Msg.loading(false);

            if(code != 200) {
                return false;
            }

            return data;
        },

        formatIncomingStatus(status) {
            let obj = Constant.INCOMING_STATUS.find(x => x.value == status);
            if(Helper.empty(obj)) {
                return "";
            }

            return obj.label;
        }
    }
}

const useStore = curlStore("company", COLUMNS, OPTIONS, SEARCH_FORM, API, store)
export default useStore
